package main

import (
	"blog/models"
	"blog/routers"
	"github.com/iris-contrib/middleware/cors"
	"github.com/kataras/iris"
	"github.com/kataras/iris/middleware/logger"
	"github.com/kataras/iris/middleware/recover"
)

func main() {
	models.Init() //初始化数据库
	app := newApp()
	routers.Router(app)         // 页面访问
	app.Run(iris.Addr(":3000")) // 火箭发射
}

func newApp() *iris.Application {
	app := iris.New()
	crs := cors.New(cors.Options{
		AllowedOrigins:   []string{"*"}, // allows everything, use that to change the hosts.
		AllowedMethods:   []string{"HEAD", "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
		AllowCredentials: true,
		AllowedHeaders:   []string{"*"},
	})
	app.Use(crs)
	app.Use(recover.New())                // 错误恢复正常中间件
	app.Use(logger.New())                 // 日志中间件
	app.Logger().SetLevel("info")         // 设置日志级别
	app.Logger().Hijack(routers.MyHijack) // 劫持日志
	app.Favicon("./assets/favicon.ico")   // 设置图标
	app.OnAnyErrorCode(routers.ErrorHandling)
	//app.Get("{name}",routers.Handler)
	//app.StaticWeb("/", "./html/dist")// 设置静态文件
	app.StaticWeb("/", "./uploads")
	app.AllowMethods(iris.MethodOptions) //跨域请求
	return app
}
